#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int left, right;
        int cnt = 0;
        for (int i = nums.size() - 1; i >= 2; i--)
        {
            left = 0;
            right = i - 1;
            while (left < right)
            {
                if (nums[left] + nums[right] > nums[i])
                {
                    cnt = cnt + (right - left);
                    right--;
                }
                else {
                    left++;
                }
            }
        }
        return cnt;
    }
};
